package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //1.检查参数
        dto.checkParam();
        //2.分页查询
        IPage page = new Page(dto.getPage(),dto.getSize());
        //lambdaQuery().like内的第一个参数是 条件 如果为true则执行查询，如果为false则不执行查询
        IPage resPage = page(page, Wrappers.<AdSensitive>lambdaQuery().like(StringUtils.isNotBlank(dto.getName()), AdSensitive::getSensitives, dto.getName()));
        //3.返回封装的结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),resPage.getTotal());
        responseResult.setData(resPage.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.检查参数
        if (adSensitive==null||StringUtils.isBlank(adSensitive.getSensitives())) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.检查需要添加的参数是否在数据库中已经存在
        //int count = count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        AdSensitive sensitive = getOne(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (adSensitive.getSensitives().equals(sensitive)){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST);
        }
        //3.执行添加操作
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        //4.返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //1.检查参数
        if (adSensitive.getId()==null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.需要修改的数据是否存在于数据库
        AdSensitive byId = getById(adSensitive.getId());
        if (byId==null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.检查敏感词是否不为空，并且和之前的敏感词不一样，以及新的敏感词是否已经存在
        if (StringUtils.isNotBlank(adSensitive.getSensitives())
                &&!adSensitive.getSensitives().equals(byId.getSensitives())){
            if (byId.getSensitives().equals(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()))) {
                CustException.cust(AppHttpCodeEnum.DATA_EXIST);
            }
        }
        //3.执行修改操作
        updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        //1.检查参数
        if (id==null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.检查需要修改的参数是否存在
        AdSensitive byId = getById(id);
        if (byId==null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.删除
        removeById(id);
        return ResponseResult.okResult();
    }
}
